package com.hao.springboot02web.controller;

import com.hao.springboot02web.dao.DepartmentDao;
import com.hao.springboot02web.dao.EmployeeDao;
import com.hao.springboot02web.pojo.Department;
import com.hao.springboot02web.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class employeeController {

    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    @RequestMapping("/list")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAllEmployee();
        model.addAttribute("emps",employees);
        return "list";
    }
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //查询所有部门信息
        Collection<Department> departments = departmentDao.getAll();
        model.addAttribute("departments",departments);
        return "add";
    }
    @PostMapping("/emp1")
    public String addEmp(Employee employee){
        System.err.println("添加员工==>"+employee);
        employeeDao.addEmployee(employee);//添加员工
        return "redirect:/list";
    }
    @RequestMapping("/emp/{id}")
    public String updateEmployee(@PathVariable("id") Integer id,Model model){
        System.out.println("id==>"+id);
        Employee employeeById = employeeDao.getEmployeeById(id);
        model.addAttribute("employee",employeeById);
        //查询所有部门信息
        Collection<Department> departments = departmentDao.getAll();
        model.addAttribute("departments",departments);
        return "update";
    }
    @PostMapping("/updateEmployee")
    public String updateEmp(Employee employee){
        employeeDao.addEmployee(employee);
        return "redirect:list";
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        employeeDao.deleteEmployeeById(id);
        return "redirect:/list";
    }
}
